package com.queclink.ycm;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2022-02-28 14:23:00
 */
public class HexUtils {

    private static final Pattern NUMBER = Pattern.compile("^\\d{1}$");

    // ASCII转Hex
    public static String asciiToHexs(String asciiStr) {
        char[] chars = asciiStr.toCharArray();
        StringBuilder hex = new StringBuilder();

        for (char ch : chars) {
            String s = Integer.toHexString((int) ch);
            Matcher matcher = NUMBER.matcher(s);
            if (matcher.find()){
                hex.append("0"+Integer.toHexString((int) ch));
            }else {
                hex.append(Integer.toHexString((int) ch).toUpperCase());
            }

        }
        return hex.toString();
    }
    // ASCII转Hex
    public static String asciiToHex(String asciiStr) {
        char[] chars = asciiStr.toCharArray();
        StringBuilder hex = new StringBuilder();
        for (char ch : chars) {
            hex.append(Integer.toHexString((int) ch));
        }
        return hex.toString();
    }

    // ASCII转Hex加空格
    public static String asciiToHexWithSpace(String asciiStr) {
        char[] chars = asciiStr.toCharArray();
        StringBuilder hex = new StringBuilder();
        for (char ch : chars) {
            hex.append(Integer.toHexString((int) ch));
            hex.append(" ");
        }
        return hex.toString();
    }

    // Hex转ASCII
    public static String hexToAscii(String hexStr) {
        StringBuilder output = new StringBuilder("");
        for (int i = 0; i < hexStr.length(); i += 2) {
            String str = hexStr.substring(i, i + 2);
            output.append((char) Integer.parseInt(str, 16));
        }
        return output.toString();
    }

    public static String fromHexString(String hexString) {
        String result = "";
        hexString = hexString.toUpperCase();
        String hexDigital = "0123456789ABCDEF";
        //将16进制转化成char数组
        char[] hexs = hexString.toCharArray();

        byte[] bytes = new byte[hexString.length() / 2];

        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = hexDigital.indexOf(hexs[2 * i]) * 16 + hexDigital.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        try {
            result = new String(bytes, "UTF-8");
        } catch (Exception e) {

        }
        return result;
    }

    /**
     * 16进制转10进制
     *
     * @param hex
     * @return
     */
    public static String hex2decimal(String hex) {
        byte[] bytes = new byte[hex.length() / 2];
        String n = "";
        for (int i = 0; i < bytes.length; i++) {
            int parseInt = Integer.parseInt(hex.substring(i * 2, (i + 1) * 2), 16);
            if (i == bytes.length - 1) {
                n = n + parseInt + "";
            } else {
                n = n + String.format("%2d", parseInt).replace(" ", "0");
            }
        }
        return n;
    }

    // Hex转ASCII
//    public static String hexToAsciiString(String hexStr) {
//        StringBuilder output = new StringBuilder("");
//        for (int i = 0; i < hexStr.length(); i += 2) {
//            String str = hexStr.substring(i, i + 2);
//            char c = (char) Integer.parseInt(str, 16);
//            if (CharUtil.isAsciiPrintable(c)){
//                output.append(c);
//            }else {
//                return "";
//            }
//
//        }
//        return output.toString();
//    }
}
